function getTransferStationForDistribute(area,id) {
    $.getJSON("../stationManage_queryStationContainsArea.json",{"area":area},function (data) {
        if(data.success==true){
            $.each(data.station,function (n,value) {
                if(n==0){
                    $("#"+id).append("<option selected value='"+value.id+"'>"+value.stationName+"</option>");
                }else{
                    $("#"+id).append("<option value='"+value.id+"'>"+value.stationName+"</option>");
                }
            });
        }
    });
}
function getAllCityForDistribute(){
    $(".city_listForDistribute option").remove();
    $(".city_listForDistribute").html("<option></option>");
    $.getJSON("../order_queryNewOrderCity.json",function(data){
        if(data.success && data.NewOrderCity != null){
            $.each(data.NewOrderCity,function(index,item){
                $(".city_listForDistribute").append("<option  value="+item+">"+item+"</option>");
            });
        }
    });
}
$(document).ready(function () {
       getAllCityForDistribute();
        var arr=[];
        var order = {};//包裹
        var orderArr =[];//包裹数组
        var id;//包裹ID
        var packageNo;//包裹号
        var orderNo;//订单编号
        var itemName;//产品名称
        var user = {};//录入者
        var stationName;//发件站点
        var destStation;//收件站点
        var streets;//站点配送范围
        var city;//城市
        var area;//区县
        var street;//街道
        var address;//详细地址
        var packageUser ={};//打包人
        var createTime;//打包时间
        $(".city_listForDistribute").change(function () {
            var city = $(".city_listForDistribute option:selected").html();
            getOrder();
        });
        function getOrder() {
            $(".remove-list").remove();
            city = $(".city_listForDistribute option:selected").html();
            if(city!=null&&city!=""){
                $.getJSON("../order_queryOrderBycity.json",{"city":city},function (data) {
                    var area;
                    $.each(data.orders,function (n,value) {
                        area = value.area;
                        $(".table-create-package").append("<tr class='remove-list'>" +
                                "<td>"+(value.isQuick?"加急件":"")+"</td>" +
                                "<td>"+value.orderNo+"</td>" +
                                "<td>"+value.user.stationName+"</td>" +
                                "<td>"+value.area+value.street+value.address+"</td>" +
                                "<td><select class=' transferStation form-control' id='"+n+"'><option></option></select></td>" +
                                "<td><input type='checkbox' class='checkBoxVal'><input type='hidden' value='"+value.id+"'></td>" +
                                "</tr>");
                        getTransferStationForDistribute(area,n);
                    });
                });
            }
        }
        $(".btn-creat-package").click(function () {
            var destStation;
            var arr;
            var first;
            var empty = "";
            var destStationArr=[];
            $(".table-create-package input:checked").each(function(){
                destStation = $(this).parent().parent().find("select option:selected").html();
                destStationArr.push(destStation);
            });
            arr = $.grep(destStationArr,function(n,i){
                return n==empty;
            });
            if(arr.length>0){
                window.wxc.xcConfirm("请选择收件站点", window.wxc.xcConfirm.typeEnum.warning);
            }else{
                first = destStationArr[0];
                arr = $.grep(destStationArr,function(n,i){
                    return n!=first;
                });
                if(arr.length>0){
                    window.wxc.xcConfirm("请打包往同一站点", window.wxc.xcConfirm.typeEnum.warning);
                }else{
                    destStation = first;
                    $(".destStation").val(destStation);
                        $(".table-create-package .checkBoxVal:checked").each(function(){
                            var obj = $(this).parent().parent();
                            obj.children().children("select").attr("disabled","disabled");
                            obj.removeClass("remove-list");
                            obj.children().children("input[type=checkbox]").replaceWith("<button class='delUnderPackage'>删除</button>");
                            $(".table-package-info").append(obj);
                            $(".delUnderPackage").unbind("click");
                            $(".delUnderPackage").click(function () {
                                var obj = $(this).parent().parent();
                                obj.children().children("select").removeAttr("disabled");
                                obj.children().children("button").replaceWith("<input type='checkbox' class='checkBoxVal'>");
                                obj.addClass("remove-list");
                                $(".table-create-package").append(obj);
                                $(".transferStation").remove("InputIsNull");
                            });
                        });
                    }
            }
        });
        $(".btn-submit-package").click(function () {
            var arr = "";
            $(".table-package-info input[type='hidden']").each(function(){
                arr += this.value + ",";
            });
            if(arr.length > 1)
                arr = arr.substring(0,arr.length - 1);
            else{
                window.wxc.xcConfirm("请先生成包裹", window.wxc.xcConfirm.typeEnum.error);
                return;
            }
            var destStation = $(".destStation").html();
            var args = {};
            args.ordersId = arr;
            args.destStation = destStation;
            $.post("../packageManage_createPackage.json",args,function (msg) {
                if(msg.success==true){
                    getAllCityForDistribute();
                    getQuickerForDistributePackage();
                    getCounterForDistributePackage();
                    $(".packageNo").val(msg.packageNo);
                    $(".table-package-info tr:gt(0)").remove();
                    window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.success);
                }else {
                    window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.error);
                }
            },"json");
        });
        $(".btn-print").click(function () {
            var jsonArr = [];
            var jsonArr1 =[];
             var filename = $(".destStation").html();
             $(".table-package-info tr:lt(1) th:lt(5)").each(function (i,value) {
                    jsonArr1.push(value.innerHTML);
             });
            jsonArr.push(jsonArr1);
            $(".table-package-info tr:gt(0)").each(function (i,value) {
            	jsonArr1=[];
                $(this).find("td:lt(5)").each(function (i,value) {
                    if(i<4){
                        jsonArr1.push(value.innerHTML);
                    }else {
                        jsonArr1.push($(this).find("select option:selected").html());
                    }
                });
                jsonArr.push(jsonArr1);
            });
            $("#export_data_form :input[name='filename']").val(filename);
            $("#export_data_form :input[name='jsonStr']").val(JSON.stringify(jsonArr));
            $("#export_data_form").submit();
        });
});
